<html>
<head>
<title> System Management & Security </title>
<link rel="stylesheet" type="text/css" href="css/html.css">
<link rel="stylesheet" type="text/css" href="css/docbook.css">
<link rel="stylesheet" type="text/css" href="css/syntax.css">
</head>


<body>
<h1> 5 UNIX Design </h1>

<div class="TOC">
<dl>
<dt><b>Table of Contents</b></dt>
<dt>5.1 <a href="chapter5.html#FILES">       Files 		    </a></dt>
<dt>5.2 <a href="chapter5.html#PROCESSES">   Processes		    </a></dt>
<dt>5.3 <a href="chapter5.html#PROCESS_CODE">Process Code 	    </a></dt>
<dt>5.4 <a href="chapter5.html#UNAME">	     The uname command 	    </a></dt>
<dt>5.5 <a href="chapter5.html#UNAME_C">     The uname() C-function </a></dt>
<dt>5.6 <a href="chapter5.html#SYSCTL">	     The sysctl command     </a></dt>
<dt>5.7 <a href="chapter5.html#ERROR">	     Errors on System Call  </a></dt>
</dl>
</div>


<!------------------ 5.1 Files ---------------------------------------->
<div class="SECT1">
<h1 class="SECT1"><a id="FILES" name="FILES">5.1 Files</a></h1>
<p>
<ul>
<li> everything is file, which means, has a file interface, e.g. </li>
<ul>
<li> plain files </li>
<li> directories </li>
<li> devices     </li>
<li> main memory </li>
</ul>
<li>users are contained in groups                   </li>
<li>files are owned by a user and a group           </li>
<li>there is one super-user with special privileges </li>
</ul>
</p>
</div>


<!------------------ 5.2 Processes ---------------------------------------->
<div class="SECT2">
<h1 class="SECT2"><a id="PROCESSES" name="PROCESSES">5.2 Processes</a></h1>
<p>
<ul>
<li>a program is an executable file, this can be</li>
<ul>
<li>a compiled program (from non executable source, e.g. C, C++) </li>
<li>an interpreted program (JAVA, Shell, Perl,...) whereas Shell, JAVA- and Perl-interpreter are compiled programs</li>
</ul>
<li>a process is a program currently executed by a processor</li>
<li>a process uses code of the programmer, C-library, operating systems</li>
<li>a process returns the value...</li>
<ul>
<li> <b>0</b> in case of <b>success</b> (true) </li>
<li> <b>1</b> in case of <b>failure</b> (false)</li>
</ul>
<li>processes are owned by the user starting it</li>
<li>group owner of the process is login group of the process owner</li>
<li>processes manipulate files</li>
<li>the process table may be viewed using the command  <kbd class="USERINPUT">ps</kbd> (process status)</li>
</ul>
</p>
</div>


<!------------------ 5.3 Process Code ---------------------------------------->
<div class="SECT3">
<h1 class="SECT3"><a id="PROCESS_CODE" name="PROCESS_CODE">5.3 Process Code</a></h1>
<p>
The operating system is a collection of system calls. A process calls the operating system (system call) via the C library interface.
</p>
<img src="img/proc-lib-system.png">
<p>
As a ordinary process a successful call returns the code 0. An unsuccessful call returns a code &lt; 0 (mostly -1).
<br>
You can use the command <kbd class="USERINPUT">ldd</kbd> to get the shared libraries which are involved in a program. To demonstrate this
command we use the following C-program:
</p>
<p><b>code listing 12: filetest.c</b></p>
<div class="CODELISTING">
<pre>
<span class="lnr"> 1 </span><span class="PreProc">#include </span><span class="Constant">&lt;stdio.h&gt;</span>
<span class="lnr"> 2 </span><span class="Comment">/*</span><span class="Comment">*</span>
<span class="lnr"> 3 </span><span class="Comment"> *  Writes &quot;hello world&quot; in the given file </span>
<span class="lnr"> 4 </span><span class="Comment"> * </span>
<span class="lnr"> 5 </span><span class="Comment"> *  name    filetest.c</span>
<span class="lnr"> 6 </span><span class="Comment"> *  version 1.0</span>
<span class="lnr"> 7 </span><span class="Comment"> *  author  Prof. Dr.-Ing. Damian Weber (dweber@htw-saarland.de)</span>
<span class="lnr"> 8 </span><span class="Comment"> </span><span class="Comment">*/</span>
<span class="lnr"> 9 </span><span class="Type">int</span> main(<span class="Type">int</span> argc, <span class="Type">char</span> **argv)
<span class="lnr">10 </span>{
<span class="lnr">11 </span>  <span class="Comment">// are there enough params?</span>
<span class="lnr">12 </span>  <span class="Statement">if</span>(argc &lt;= <span class="Constant">1</span>)
<span class="lnr">13 </span>  {
<span class="lnr">14 </span>    <span class="Statement">return</span> <span class="Constant">1</span>;
<span class="lnr">15 </span>  }
<span class="lnr">16 </span>
<span class="lnr">17 </span>  <span class="Type">FILE</span> *filePointer = <span class="Constant">0</span>;
<span class="lnr">18 </span>
<span class="lnr">19 </span>  filePointer = fopen(argv[<span class="Constant">1</span>],<span class="Constant">&quot;w&quot;</span>);
<span class="lnr">20 </span>  <span class="Comment">// has fopen been successful?</span>
<span class="lnr">21 </span>  <span class="Statement">if</span>(!filePointer)
<span class="lnr">22 </span>  {
<span class="lnr">23 </span>     <span class="Statement">return</span> <span class="Constant">1</span>;
<span class="lnr">24 </span>  }
<span class="lnr">25 </span>
<span class="lnr">26 </span>  <span class="Comment">// writes &quot;hello worldn&quot; in the given file (argv[1])</span>
<span class="lnr">27 </span>  fprintf(filePointer, <span class="Constant">&quot;hello world</span><span class="Special">n</span><span class="Constant">&quot;</span>);
<span class="lnr">28 </span>  fclose(filePointer);
<span class="lnr">29 </span>
<span class="lnr">30 </span>  <span class="Statement">return</span> <span class="Constant">0</span>;
<span class="lnr">31 </span>}
</pre>
</div>
<br>

<kbd class="USERINPUT">ldd</kbd> will show us a result like the the following:

<div class="CODELISTING">
<pre>
$ ldd filetest
linux-gate.so.1 =>  (0xb7f7f000)
libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb7df4000)
/lib/ld-linux.so.2 (0xb7f65000)
</pre>
</div>


</div>


<!------------------ 5.4 The uname command ---------------------------------------->
<div class="SECT4">
<h1 class="SECT4"><a id="UNAME" name="UNAME">5.4 The uname commmand</a></h1>
<p>
The uname command writes the name of the operating system implementation to standard output.

<p><b> uname example 1: version level of this release of the operating system</b></p>
<div class="CODELISTING">
<pre>
$ uname -v
#1 SMP Thu Nov 20 21:57:00 UTC 2008
</pre>
</div>

<p><b> uname example 2: current release level of the operating system </b></p>
<div class="CODELISTING">
<pre>
$ uname -r
2.6.27-9-generic
</pre>
</div>

<p><b> uname example 3: type of the current hardware platform </b></p>
<div class="CODELISTING">
<pre>
$ uname -m
i686
</pre>
</div>

<p><b> uname example 4: all of the uname information </b></p>
<div class="CODELISTING">
<pre>
$ uname -a
Linux schlepptopFromHell 2.6.27-9-generic #1 SMP Thu Nov 20 21:57:00 UTC 2008 i686 GNU/Linux
</pre>
</div>


</p>
<small>
Reference <a href="http://www.freebsd.org/cgi/man.cgi?query=uname&apropos=0&sektion=0&manpath=FreeBSD+7.0-RELEASE&format=html">uname manual page</a>
</small>
</div>


<!------------------ 5.5 The uname() C-function ---------------------------------------->
<div class="SECT5">
<h1 class="SECT5"><a id="UNAME_C" name="UNAME_C">5.5 The uname() C-function</a></h1>
<p>
<div class="CODELISTING">
<pre>
UNAME(3)	       FreeBSD Library Functions Manual 	      UNAME(3)

NAME
uname -- get system identification

LIBRARY
Standard C Library (libc, -lc)

SYNOPSIS
#include  &lt;sys/utsname.h&gt;

int
uname(struct utsname *name);

DESCRIPTION
The uname() function stores NUL-terminated strings of information identi-
fying the current system into the structure referenced by name.

The utsname structure is defined in the &lt;sys/utsname.h&gt; header file, and
contains the following members:

sysname	 Name of the operating system implementation.

nodename Network name of this machine.

release	 Release level of the operating system.

version	 Version level of the operating system.

machine	 Machine hardware platform.

RETURN VALUES
The uname() function returns the value 0 if successful; otherwise the
value -1 is returned and the global variable errno is set to indicate the
error.

</pre>
</div>

<p><b>code listing 13: <tt class="FILENAME">/usr/include/sys/utsname.h </tt></b></p>
<div class="CODELISTING">
<pre>
<span class="lnr"> 1 </span><span class="PreProc">#ifndef SYS_NMLN</span>
<span class="lnr"> 2 </span><span class="PreProc">#   define SYS_NMLN </span><span class="Constant">256</span><span class="PreProc"> </span><span class="Comment">/*</span><span class="Comment"> User can override </span><span class="Comment">*/</span>
<span class="lnr"> 3 </span><span class="PreProc">#endif</span>
<span class="lnr"> 4 </span>
<span class="lnr"> 5 </span><span class="Comment">/*</span><span class="Comment"> Structure describing the system and machine.  </span><span class="Comment">*/</span>
<span class="lnr"> 6 </span><span class="Type">struct</span> utsname
<span class="lnr"> 7 </span>{
<span class="lnr"> 8 </span>    <span class="Comment">/*</span><span class="Comment"> Name of the implementation of the operating system.  </span><span class="Comment">*/</span>
<span class="lnr"> 9 </span>    <span class="Type">char</span> sysname[SYS_NMLN];
<span class="lnr">10 </span>
<span class="lnr">11 </span>    <span class="Comment">/*</span><span class="Comment"> Name of this node on the network.  </span><span class="Comment">*/</span>
<span class="lnr">12 </span>    <span class="Type">char</span> nodename[SYS_NMLN];
<span class="lnr">13 </span>
<span class="lnr">14 </span>    <span class="Comment">/*</span><span class="Comment"> Current release level of this implementation.  </span><span class="Comment">*/</span>
<span class="lnr">15 </span>    <span class="Type">char</span> release[SYS_NMLN];
<span class="lnr">16 </span>
<span class="lnr">17 </span>    <span class="Comment">/*</span><span class="Comment"> Current version level of this release.  </span><span class="Comment">*/</span>
<span class="lnr">18 </span>    <span class="Type">char</span> version[SYS_NMLN];
<span class="lnr">19 </span>
<span class="lnr">20 </span>    <span class="Comment">/*</span><span class="Comment"> Name of the hardware type the system is running on.  </span><span class="Comment">*/</span>
<span class="lnr">21 </span>    <span class="Type">char</span> machine[SYS_NMLN];
<span class="lnr">22 </span>}
</pre>
</div>

<p><b>code listing 14: A uname C program</b></p>
<div class="CODELISTING">
<pre>
<span class="lnr"> 1 </span><span class="PreProc">#include </span><span class="Constant">&lt;sys/utsname.h&gt;</span>
<span class="lnr"> 2 </span><span class="PreProc">#include </span><span class="Constant">&lt;stdio.h&gt;</span>
<span class="lnr"> 3 </span><span class="Comment">/*</span><span class="Comment">*</span>
<span class="lnr"> 4 </span><span class="Comment"> *  Prints system information on the stdout</span>
<span class="lnr"> 5 </span><span class="Comment"> *</span>
<span class="lnr"> 6 </span><span class="Comment"> *  name    utsname_test.c</span>
<span class="lnr"> 7 </span><span class="Comment"> *  version 1.0</span>
<span class="lnr"> 8 </span><span class="Comment"> *  author  Prof. Dr.-Ing. Damian Weber (dweber@htw-saarland.de)</span>
<span class="lnr"> 9 </span><span class="Comment"> </span><span class="Comment">*/</span>
<span class="lnr">10 </span><span class="Type">int</span> main()
<span class="lnr">11 </span>{
<span class="lnr">12 </span>  <span class="Type">struct</span> utsname sysInfo;
<span class="lnr">13 </span>
<span class="lnr">14 </span>  <span class="Statement">if</span>(uname(&amp;sysInfo) == <span class="Constant">0</span>)
<span class="lnr">15 </span>  {
<span class="lnr">16 </span>    puts(<span class="Constant">&quot;system information:&quot;</span>);
<span class="lnr">17 </span>    printf(<span class="Constant">&quot;sysname:  </span><span class="Special">%s</span><span class="Special">n</span><span class="Constant">&quot;</span>, sysInfo.sysname);
<span class="lnr">18 </span>    printf(<span class="Constant">&quot;nodename: </span><span class="Special">%s</span><span class="Special">n</span><span class="Constant">&quot;</span>, sysInfo.nodename);
<span class="lnr">19 </span>    printf(<span class="Constant">&quot;release:  </span><span class="Special">%s</span><span class="Special">n</span><span class="Constant">&quot;</span>, sysInfo.release);
<span class="lnr">20 </span>    printf(<span class="Constant">&quot;version:  </span><span class="Special">%s</span><span class="Special">n</span><span class="Constant">&quot;</span>, sysInfo.version);
<span class="lnr">21 </span>    printf(<span class="Constant">&quot;machine:  </span><span class="Special">%s</span><span class="Special">n</span><span class="Constant">&quot;</span>, sysInfo.machine);
<span class="lnr">22 </span>  }
<span class="lnr">23 </span>  <span class="Statement">else</span>
<span class="lnr">24 </span>  {
<span class="lnr">25 </span>    <span class="Comment">// errno is already set</span>
<span class="lnr">26 </span>    perror(<span class="Constant">&quot;uname&quot;</span>);
<span class="lnr">27 </span>    <span class="Statement">return</span> <span class="Constant">1</span>;
<span class="lnr">28 </span>  }
<span class="lnr">29 </span>
<span class="lnr">30 </span>  <span class="Statement">return</span> <span class="Constant">0</span>;
<span class="lnr">31 </span>}
</pre>
</div>

</p>
<small>
Reference <a href="http://www.freebsd.org/cgi/man.cgi?query=uname&apropos=0&sektion=3&manpath=FreeBSD+7.0-RELEASE&format=html">uname() C-function manual page</a>
</small>
</div>


<!------------------ 5.6 The sysctl command ---------------------------------------->
<div class="SECT6">
<h1 class="SECT6"><a id="SYSCTL" name="SYSCTL">5.6 The sysctl commmand</a></h1>
<p>
The sysctl utility retrieves kernel state and allows processes with appropriate privilege to set kernel state.

<p><b>examples for using sysctl</b></p>
<div class="CODELISTING">
<pre>
$ sysctl kern.ostype
kern.ostype: FreeBSD

$ sysctl kern.osrelease
kern.osrelease: 6.3-RELEASE-p2

$ sysctl hw.model
hw.model: Intel(R) Pentium(R) 4 CPU 1.80 GHz
</pre>
</div>
</p>

<p>Of course there is a matching C-function <a href="http://www.freebsd.org/cgi/man.cgi?query=sysctl&apropos=0&sektion=3&manpath=FreeBSD+7.0-RELEASE&format=html"> sysctl() </a>.</p>

<small>
Reference <a href="http://www.freebsd.org/cgi/man.cgi?query=sysctl&apropos=0&sektion=0&manpath=FreeBSD+7.0-RELEASE&format=html">sysctl manual page</a>
</small>
</div>


<!------------------ 5.7 Errors on System Call ---------------------------------------->
<div class="SECT7">
<h1 class="SECT7"><a id="ERROR" name="ERROR">5.7 Errors on System Call</a></h1>
<p>If a system call fails it usually returns -1. <b>Therefore it is strongly recommended to check all return values of all function calls for safety reasons.</b> Additionally the system call sets a global variable  <kbd class="USERINPUT"> int errno </kbd> which is defined in  <tt class="FILENAME">errno.h </tt>. All possible values of errno are given by constants starting with "E" e.g. ENOMEM (Cannot allocate memory), EACCES (Permission denied), EINVAL (Invalid argument)... . For a detailed introduction of system calls and error numbers and a complete list of the errors and their names have a look at the <a href="http://www.freebsd.org/cgi/man.cgi?query=errno&apropos=0&sektion=0&manpath=FreeBSD+7.0-RELEASE&format=html">FreeBSD System Calls Manual</a>. </p>

<p>There are the functions <kbd class="USERINPUT">perror()</kbd> and <kbd class="USERINPUT">strerror()</kbd> which look up the error
message string corresponding to an error number. <kbd class="USERINPUT">perror()</kbd> prints the error text of the current value of <kbd class="USERINPUT">errno</kbd>. <kbd class="USERINPUT">strerror()</kbd> creates a string containing the error text. For detailed description of <kbd class="USERINPUT">perror()</kbd>and <kbd class="USERINPUT">strerror()</kbd> have look at the manual page <a href="http://www.freebsd.org/cgi/man.cgi?query=perror&apropos=0&sektion=0&manpath=FreeBSD+7.0-RELEASE&format=html">STRERROR(3)</a>.

<p><b>synopsis of perror() and strerror()</b></p>
<div class="CODELISTING">
<pre>
#include &lt;stdio.h&gt;

void
perror(const char *string);


#include &lt;string.h&gt;

char *
strerror(int errnum);
</pre>
</div>
</p>

<p>The example below demonstrates the mentioned possibilites of error handling:

<p><b>code listing 14: errno_test.c</b></p>
<div class="CODELISTING">
<pre>
	<span class="lnr"> 1 </span><span class="PreProc">#include </span><span class="Constant">&lt;errno.h&gt;</span>
	<span class="lnr"> 2 </span><span class="PreProc">#include </span><span class="Constant">&lt;string.h&gt;</span>
	<span class="lnr"> 3 </span><span class="PreProc">#include </span><span class="Constant">&lt;stdio.h&gt;</span>
	<span class="lnr"> 4 </span>
	<span class="lnr"> 5 </span><span class="Comment">/*</span><span class="Comment">*</span>
	<span class="lnr"> 6 </span><span class="Comment"> *  Demonstrates the error handling with the help of errno, perror() and strerror</span>
	<span class="lnr"> 7 </span><span class="Comment"> * </span>
	<span class="lnr"> 8 </span><span class="Comment"> *  name    errno_test.c</span>
	<span class="lnr"> 9 </span><span class="Comment"> *  version 1.0</span>
	<span class="lnr">10 </span><span class="Comment"> *  author  Benjamin Friedrich (pib.benjamin.friedrich@htw-saarland.de)</span>
	<span class="lnr">11 </span><span class="Comment"> </span><span class="Comment">*/</span>
	<span class="lnr">12 </span><span class="Type">int</span> main()
	<span class="lnr">13 </span>{
	<span class="lnr">14 </span>  <span class="Comment">// activates an error by using &quot;invalid mode&quot; instead of &quot;r&quot;, &quot;r+&quot;,.. </span>
	<span class="lnr">15 </span>  <span class="Type">FILE</span> *file = fopen(<span class="Constant">&quot;test_file.txt&quot;</span>,<span class="Constant">&quot;invalid mode&quot;</span>);
	<span class="lnr">16 </span>
	<span class="lnr">17 </span>  <span class="Comment">// if fopen returns NULL there has been an error</span>
	<span class="lnr">18 </span>  <span class="Statement">if</span>(file == <span class="Constant">NULL</span>)
	<span class="lnr">19 </span>  {
	<span class="lnr">20 </span>     <span class="Comment">/*</span><span class="Comment"> it is recommended to store the errno if perror() is not called directly after an occured error, because the value</span>
	<span class="lnr">21 </span><span class="Comment">        of errno is not defined after a successful call of a library function. So it is possible the value of errno could be</span>
	<span class="lnr">22 </span><span class="Comment">        changed by such a other function call.</span><span class="Comment">*/</span>
	<span class="lnr">23 </span>     <span class="Type">int</span> savedErrNo = errno;
	<span class="lnr">24 </span>
	<span class="lnr">25 </span>     <span class="Comment">// prints the error message with perror()</span>
	<span class="lnr">26 </span>     perror(<span class="Constant">&quot;fopen (perror)&quot;</span>);
	<span class="lnr">27 </span>
	<span class="lnr">28 </span>     <span class="Comment">// handles the error message with strerror(). </span>
	<span class="lnr">29 </span>     <span class="Type">char</span> *errorMessage = strerror(savedErrNo);
	<span class="lnr">30 </span>     printf(<span class="Constant">&quot;fopen (strerror): </span><span class="Special">%s</span><span class="Special">n</span><span class="Constant">&quot;</span>,errorMessage);
	<span class="lnr">31 </span>
	<span class="lnr">32 </span>     <span class="Comment">// error handling depending on the occured error</span>
	<span class="lnr">33 </span>     <span class="Statement">switch</span>(savedErrNo)
	<span class="lnr">34 </span>     {
	<span class="lnr">35 </span>        <span class="Statement">case</span> <span class="Constant">EINVAL</span>: printf(<span class="Constant">&quot;fopen (EINVAL): invalid mode as it is mentioned in the man page of fopen</span><span class="Special">n</span><span class="Constant">&quot;</span>);
	<span class="lnr">36 </span>                     <span class="Statement">break</span>;
	<span class="lnr">37 </span>        <span class="Statement">default</span>:     printf(<span class="Constant">&quot;fopen: Any other error: </span><span class="Special">%i</span><span class="Special">n</span><span class="Constant">&quot;</span>,savedErrNo);
	<span class="lnr">38 </span>     }
	<span class="lnr">39 </span>
	<span class="lnr">40 </span>     <span class="Comment">// signalises there has been an error</span>
	<span class="lnr">41 </span>     <span class="Statement">return</span> <span class="Constant">1</span>;
	<span class="lnr">42 </span>  }
	<span class="lnr">43 </span>
	<span class="lnr">44 </span>  <span class="Comment">// there has not been any error...  </span>
	<span class="lnr">45 </span>  fprintf(file, <span class="Constant">&quot;no idea how this work...&quot;</span>);
	<span class="lnr">46 </span>  fclose(file);
	<span class="lnr">47 </span>
	<span class="lnr">48 </span>  <span class="Statement">return</span> <span class="Constant">0</span>;
	<span class="lnr">49 </span>}
</pre>
</div>
</p>
<small>
References <a href="http://www.freebsd.org/cgi/man.cgi?query=errno&apropos=0&sektion=0&manpath=FreeBSD+7.0-RELEASE&format=html">FreeBSD System Calls Manual</a>; <a href="http://www.freebsd.org/cgi/man.cgi?query=perror&apropos=0&sektion=0&manpath=FreeBSD+7.0-RELEASE&format=html">STRERROR(3)</a>.
</small>

</div>

</body>
